<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="org.apache.commons.codec.digest.DigestUtils" %>
<%@ page import="org.apache.http.HttpEntity" %>
<%@ page import="org.apache.http.HttpResponse" %>
<%@ page import="org.apache.http.client.HttpClient" %>
<%@ page import="org.apache.http.client.methods.HttpPost" %>
<%@ page import="org.apache.http.entity.StringEntity" %>
<%@ page import="org.apache.http.util.EntityUtils" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.interfaces.cetc.action.util.HttpUtil" %>
<%@ page import="java.security.SecureRandom" %>
<%@ page import="java.text.ParsePosition" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Random" %>
<%@ page import="java.util.regex.Matcher" %>
<%@ page import="java.util.regex.Pattern" %>
<%
    JSONObject jsonData = new JSONObject(  );
    String APPID = "Zkxmh";
    String URL = "http://cj.zym.red/portal/index/apinotice";
    String ENCODE = "UTF-8";
    Map<String, String> header = new HashMap<>();
    String nonceStr = getRandomNumber();
    String curdate = DateUtil.getFullDate();
    String timestamp = HttpUtil.dateToStamp( curdate );
    String md5str = encryptToMD5( APPID + timestamp );
    String sign = encryptToMD5( md5str + nonceStr );
    header.put( "timestamp", timestamp );
    header.put( "nonceStr", nonceStr );
    header.put( "sign", sign );//使用appId+timestamp进行MD5后再拼接nonceStr字符串进行MD5加密
    System.out.println( "header=" + header.toString() );
    Map<String, String> body = new HashMap<>();
    body.put( "type", "pre" );//不传或传pre表示预览 release表示正式发版
    body.put( "title", "测试公告标题" );//公告标题
    body.put( "author", "张三" );//公告申请人
    body.put( "department", "广告部门" );//公告申请部门
    body.put( "tel", "10086" );//联系方式
    body.put( "apptime", HttpUtil.dateToStamp( "2022-04-29 11:54:20" ) );//申请时间 时间戳
    body.put( "content", "<div id=\"wea_rich_text_default_font\" style=\"font-family:微软雅黑;font-size:12;\"><p>测试</p></div>" );//公告正文含富文本标签,img内容直接传递base64在标签中
    System.out.println( "body=" + body.toString() );
    String unicodeResult = "";
    String result = unicodeToCN( unicodeResult );
    System.out.println( curdate );
    System.out.println( result );
    jsonData.put("data",result);
    out.print( jsonData.toJSONString());
%>
<%!
    public static String doPostJson(HttpClient httpClient, String url, String strJson, String charset, Map<String, String> headers) throws Exception {
        String result = null;
        url = "http://wportal.cetccks.com/zkxmh/public/portal/index/apinotice";
        HttpPost httpPost = new HttpPost(url);
//         httpPost.setHeader("Content-Type", "text/html;charset=GBK");
        httpPost.addHeader("Content-Type", "application/json;");
        //设置header
        if (headers != null && headers.size() > 0) {
            for (Map.Entry<String, String> entry : headers.entrySet()) {
                httpPost.addHeader( entry.getKey(), entry.getValue() );
            }
        }
        StringEntity entity = new StringEntity(strJson);
        httpPost.setEntity(entity);
        HttpResponse response = httpClient.execute(httpPost);
        if (response != null) {
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                result = EntityUtils.toString(resEntity, charset);
            }
        }
        return result;
    }
    /**
     * 时间转换成时间戳,参数和返回值都是字符串
     *
     * @param s
     * @return res
     */
    public static String dateToStamp(String s) {
        String res = "";
        //设置时间模版
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
            Date date = simpleDateFormat.parse( s, new ParsePosition( 0 ) );
            long ts = date.getTime() / 1000;
            res = String.valueOf( ts );
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res;
    }

    //MD5
    public static String encryptToMD5(String str) {
        return DigestUtils.md5Hex( str );
    }
    /**
     * Unicode转 汉字字符串
     *
     * @param str
     * @return
     */
    public static String unicodeToCN(String str) {
        Pattern pattern = Pattern.compile( "(\\\\u(\\p{XDigit}{4}))" );
        Matcher matcher = pattern.matcher( str );
        char ch;
        while (matcher.find()) {
            ch = (char) Integer.parseInt( matcher.group( 2 ), 16 );
            str = str.replace( matcher.group( 1 ), ch + "" );
        }
        return str;
    }

    /**
     * 获取长度为 6 的随机字母+数字
     *
     * @return 随机数字
     */
    public static String getRandomNumber() {
        String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 数字和26个字母组成
        Random random = new SecureRandom();
        char[] nonceChars = new char[6];  //指定长度为6位/自己可以要求设置
        for (int index = 0; index < nonceChars.length; ++index) {
            nonceChars[index] = SYMBOLS.charAt( random.nextInt( SYMBOLS.length() ) );
        }
        return new String( nonceChars );
    }


%>